package com.reader.cms.controller.topic;

import com.reader.cms.controller.BaseController;
import com.reader.cms.entity.topic.ReaderTopic;
import com.reader.cms.service.topic.IReaderTopicService;
import org.slf4j.Logger;
import org.springframework.web.bind.annotation.*;
import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import com.reader.core.entity.ResultMessageInfo;
import org.slf4j.LoggerFactory;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import com.reader.core.utils.ValidatorUtil;
import com.reader.core.validator.group.AddGroup;
import com.baomidou.mybatisplus.plugins.Page;
import org.springframework.transaction.annotation.Transactional;

/**
 * @description   功能描述
 * @author        gumenghao
 * @date          2018-11-06
 */
@RestController
@RequestMapping("/api/readertopic")
public class ReaderTopicController extends BaseController {
private final Logger logger = LoggerFactory.getLogger(ReaderTopicController.class);

@Autowired
public IReaderTopicService iReaderTopicService;


/**
 * @function 分页查询数据
 *
 * @param currentPage   当前页
 * @param size   每页记录数
 * @param title  专题名称
 */
@GetMapping("list")
public ResultMessageInfo selectListPage(@RequestParam(value = "currentPage") int currentPage ,
                                                @RequestParam(value = "size") int size,
                                                @RequestParam(value = "title",required = false) String title) {
       Page<ReaderTopic> list=iReaderTopicService.selectListPage(currentPage,size,title);
       return ResultMessageInfo.ofSuccess("查询成功！", list);
}

/**
 * @function 添加新记录
 * @param readerTopic 实体对象
 * @return
 */
@PostMapping("insert")
public ResultMessageInfo insert(@RequestBody ReaderTopic readerTopic) {
    ValidatorUtil.validateEntity(readerTopic, AddGroup.class);//非空验证
    String id = UUID.randomUUID().toString().replace("-", "");
    readerTopic.setId(id);
    Integer userId = this.getUserId();
    readerTopic.setInsertDt(new Date());
    readerTopic.setDelFlag(0);
    readerTopic.setInsertUserId(userId);
    readerTopic.setUpdtUserId(userId);
    boolean resultValue =  iReaderTopicService.insertTopic(readerTopic);
     if(!resultValue){
           ResultMessageInfo.ofMessage("添加失败！");
     }
     return  ResultMessageInfo.ofSuccess("添加成功！");
 }

/**
 * @function 修改
 * @param readerTopic 实体对象
 * @return
 */
@PostMapping("update")
@Transactional
public ResultMessageInfo update(@RequestBody  ReaderTopic readerTopic) {
    ValidatorUtil.validateEntity(readerTopic, AddGroup.class);//非空验证
    Integer userId = this.getUserId();
    readerTopic.setUpdtDt(new Date());
    readerTopic.setUpdtUserId(userId);
    boolean resultValue =  iReaderTopicService.updateTopic(readerTopic);
    if (!resultValue) {
        return ResultMessageInfo.ofMessage("修改失败！");
    }
    return ResultMessageInfo.ofSuccess("修改成功！");
}

/**
 * @function 单条删除
 * @param readerTopic 键为id值为整型
 * @return
 */
 @PostMapping("delete")
 @Transactional
 public ResultMessageInfo deleteById(@RequestBody ReaderTopic readerTopic){
    Integer userId = this.getUserId();
    readerTopic.setDelFlag(0);
    readerTopic.setUpdtDt(new Date());
    readerTopic.setUpdtUserId(userId);
    boolean resultValue =  iReaderTopicService.deleteById(readerTopic.getId());
    if (!resultValue) {
        ResultMessageInfo.ofMessage("删除失败！");
    }
    return ResultMessageInfo.ofSuccess("删除成功！");

 }

/**
 * @function 批量删除对象
 * @param data 键为ids值为整型数组的json格式
 * @return
 */
 @PostMapping("deleteIds")
 @Transactional
 public ResultMessageInfo deleteBatchIds(@RequestBody String data){
    List<Long> ids = JSON.parseArray( JSON.parseObject(data).getString("ids"),Long.class);
    boolean resultValue =  iReaderTopicService.deleteBatchIds(ids);
    if (!resultValue) {
        ResultMessageInfo.ofMessage("删除失败！");
    }
    return ResultMessageInfo.ofSuccess("删除成功！");
 }
}